// 防抖
export function debounce(func: Function, delay: number) {
	let timer: any;
	return function (this: any, ...args: any[]) {
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(() => {
			func.apply(this, args);
		}, delay);
	};
}
// 节流
export function throttle(func: Function, delay: number) {
	let timer: any;
	return function (this: any, ...args: any[]) {
		if (!timer) {
			timer = setTimeout(() => {
				func.apply(this, args);
				timer = null;
			}, delay);
		}
	};
}
export function generateRandomString(length: number): string {
	const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	let result = '';
	for (let i = 0; i < length; i++) {
		result += characters.charAt(Math.floor(Math.random() * characters.length));
	}
	return result;
}
export function getElementPosition(element: HTMLElement): { top: number, left: number } {
	const rect = element.getBoundingClientRect();
	return {
		top: rect.top + window.pageYOffset,
		left: rect.left + window.pageXOffset
	};
}
export function flattenTree(tree: any[]): any[] {
	const result: any[] = [];
	tree.forEach((node) => {
		result.push(node);
		if (node.children) {
			result.push(...flattenTree(node.children));
		}
	});
	return result;
}




